<template>
  <el-form ref="form" :model="formData" class="editForm">
    <el-collapse v-model="activeNames">
      <h4>全局设置</h4>
      <el-collapse-item title="展示设置" name="1">
        <el-radio-group v-model="formData.showDevice">
          <el-radio label="all">PC端和手机端</el-radio>
          <el-radio label="desktop">仅PC端</el-radio>
          <el-radio label="mobile">仅手机端</el-radio>
        </el-radio-group>
      </el-collapse-item>
      <el-collapse-item title="遮罩" name="2">
        <el-form-item label="颜色">
          <el-color-picker
              v-model="formData.maskColor"
              show-alpha
              :predefine="predefineColors">
          </el-color-picker>
        </el-form-item>
      </el-collapse-item>
      <h4>背景</h4>
      <el-collapse-item title="底图" name="9">
        <el-form-item label="底色">
          <el-color-picker
              v-model="formData.baseMap1"
              :predefine="predefineColors">
          </el-color-picker>
        </el-form-item>
        <el-form-item label="图片" prop="imageUrl" class="no-border pic">
          <ele-form-upload-plus-file v-model="formData.imageUrl" title="图片" ref="imageSelect"   />
        </el-form-item>
        <el-form-item label="样式" class="editForm-item" style="margin: 20px" v-if="formData.imageUrl">
          <el-select v-model="formData.bgStyle" placeholder="样式">
            <el-option label="自适应" value="bg-selfAdaption"></el-option>
            <el-option label="填充" value="bg-fill"></el-option>
            <el-option label="拉伸" value="bg-stretching"></el-option>
            <el-option label="平铺" value="bg-tile"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="对齐" class="editForm-item" style="margin: 20px" v-if="formData.imageUrl">
          <el-select v-model="formData.bgAlignment" placeholder="对齐">
            <el-option label="居中" value="bg-center"></el-option>
            <el-option label="左上" value="bg-left-top"></el-option>
            <el-option label="右上" value="bg-right-top"></el-option>
            <el-option label="左下" value="bg-left-bottom"></el-option>
            <el-option label="右下" value="bg-right-bottom"></el-option>
          </el-select>
        </el-form-item>
      </el-collapse-item>
      <h4>内容</h4>
      <el-collapse-item name="3">
        <template slot="title">
          <div class="collapse-item-nowrap">{{'文本：' + headerText.popupTitle}}</div>
        </template>
        <el-form-item label="" class="editForm-item">
          <quill-editor v-model="formData.popupTitle"
                        ref="myQuillEditor"
                        @ready="onEditorChange($event, 'popupTitle')"
          >
          </quill-editor>
        </el-form-item>
      </el-collapse-item>
      <el-collapse-item name="4">
        <template slot="title">
          <div class="collapse-item-nowrap">{{'文本：' + headerText.programme}}</div>
        </template>
        <el-form-item label="">
          <quill-editor v-model="formData.programme"
                        ref="myQuillEditor"
                        @ready="onEditorChange($event, 'programme')"
          >
          </quill-editor>
        </el-form-item>
      </el-collapse-item>
      <el-collapse-item name="6">
        <template slot="title">
          <div class="collapse-item-nowrap">{{'按钮：' + headerText.btnText}}</div>
        </template>
        <el-form-item label="">
          <quill-editor v-model="formData.btnText"
                        ref="myQuillEditor"
                        @ready="onEditorChange($event, 'btnText')"
          >
          </quill-editor>
        </el-form-item>
        <el-form-item label="底色">
          <el-color-picker
              v-model="formData.btnBackgroundColor"
              :predefine="predefineColors">
          </el-color-picker>
        </el-form-item>
        <el-form-item label="边框颜色">
          <el-color-picker
              v-model="formData.btnBorderColor"
              :predefine="predefineColors">
          </el-color-picker>
        </el-form-item>
        <el-form-item label="活动跳转" class="editForm-item" prop="link">
          <el-input type="textarea" class="editForm-item-area" v-model="formData.link"></el-input>
        </el-form-item>
      </el-collapse-item>
    </el-collapse>
    <el-form-item style="text-align: right;margin-top: 20px">
      <el-button @click="handleResetStyle()">重置样式</el-button>
      <el-button @click="$emit('on-cancel')">取消</el-button>
      <el-button type="primary" @click="onSubmit">保存</el-button>
    </el-form-item>
  </el-form>
</template>

<script>
import configDefaultItems from '_c/theme-editor/config/subscriptionPopus';

export default {
    name: 'Form',
    props: {
        value: {
            type: Object,
            default: () => ({})
        },
        resetStyle: {
            type: Boolean,
            default: false
        }
    },
    data () {
        return {
            // 简单copy数据
            activeNames: [],
            formData: this.$props.value ? JSON.parse(JSON.stringify(this.$props.value)) : {},
            formDataBase: this.$props.value ? JSON.parse(JSON.stringify(this.$props.value)) : {},
            predefineColors: [
                '#ff4500',
                '#ff8c00',
                '#ffd700',
                '#90ee90',
                '#00ced1',
                '#1e90ff',
                '#c71585',
                'rgba(255, 69, 0, 0.68)',
                'rgb(255, 120, 0)',
                'hsv(51, 100, 98)',
                'hsva(120, 40, 94, 0.5)',
                'hsl(181, 100%, 37%)',
                'hsla(209, 100%, 56%, 0.73)'
            ],
            headerText: {
                popupTitle: '',
                programme: '',
                description: '',
                btnText: ''
            },
            rules: {
                link: [
                    {required: true, message: '请输入活动链接', trigger: 'blur'}
                ]
            }
        };
    },
    watch: {
        formData: {
            deep: true,
            handler (value) {
                this.$emit('input', value);
                this.$emit('on-change', value);
            }
        }
    },
    methods: {
        onEditorChange (quill, name) {
            this.headerText[name] = quill.container.innerText;
        },
        async onSubmit () {
            await this.$refs.form.validate();
            console.log(this.formData);
            this.$emit('on-submit', this.formData);
        },
        handleResetStyle () {
            if (this.$props.resetStyle) {
                let datalist = configDefaultItems.filter(v => v.name === 'MarketingActivities');
                Object.assign(this.formData, datalist[0].value);
            } else {
                Object.assign(this.formData, this.formDataBase);
            }
        }
    }
};
</script>

<style scoped lang="scss">
.game-select{
  .selected {
    height: 32px;
    border-radius: 2px;
    background-color: #eef0f2;
    line-height: 32px;
    display: flex;
    padding-left: 10px;
    align-items: center;
    .value {
      flex: 1;
      height: 100%;
      overflow: hidden;
      text-overflow: ellipsis;
      word-break: break-all;
      white-space: nowrap;
    }
    i {
      width: 32px;
    }
  }
}
.bg-selfAdaption {
  background-repeat: no-repeat!important;
  background-size: auto 100% !important;
}
</style>
